package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.RP;
import org.example.studentsystem_back.domain.StudentRP;
import org.example.studentsystem_back.domain.Students;
import org.example.studentsystem_back.domain.Users;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.mapper.StudentRPMapper;
import org.example.studentsystem_back.mapper.StudentsMapper;
import org.example.studentsystem_back.mapper.UsersMapper;
import org.example.studentsystem_back.service.RPService;
import org.example.studentsystem_back.mapper.RPMapper;
import org.example.studentsystem_back.service.StudentsService;
import org.example.studentsystem_back.vo.rewardMangeVo.PageRewardVo;
import org.example.studentsystem_back.vo.rewardMangeVo.smallVo.PageRewardItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
* @author Lenovo
* @description 针对表【rewards_punishments】的数据库操作Service实现
* @createDate 2024-07-12 08:27:46
*/
@Service
public class RPServiceImpl extends ServiceImpl<RPMapper, RP>
    implements RPService{
    @Resource
    RPMapper rpMapper;
    @Resource
    StudentRPMapper studentRPMapper;
    @Resource
    StudentsMapper studentsMapper;
    @Resource
    UsersMapper usersMapper;
    @Autowired
    private StudentRPServiceImpl studentRPServiceImpl;

    @Override
    public CommonResponse<PageRewardVo> getRPsByPage(PageDTO pageDTO) {
        IPage<RP> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<RP> rpList = rpMapper.selectList(page,null);
        long total = rpMapper.selectCount(null);

        List<PageRewardItem> itemList = new ArrayList<>();
        rpList.forEach(rp -> {
            PageRewardItem pageRewardItem = new PageRewardItem();
            pageRewardItem.setDataByRP(rp);
            QueryWrapper<StudentRP> studentRPQueryWrapper = new QueryWrapper<>();
            studentRPQueryWrapper.eq("reward_punishment_id",rp.getId());
            StudentRP studentRP = studentRPMapper.selectOne(studentRPQueryWrapper);
            QueryWrapper<Students> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("student_id",studentRP.getStudentId());
            Students student = studentsMapper.selectOne(studentQueryWrapper);
            Users users = usersMapper.selectById(student.getUserid());
            pageRewardItem.setName(users.getName());
            itemList.add(pageRewardItem);
        });
        PageRewardVo pageRewardVo = new PageRewardVo();
        pageRewardVo.setPageList(itemList);
        pageRewardVo.setTotal(total);
        return CommonResponse.createForSuccess(pageRewardVo);
    }

}




